package com.ruoyi.web.controller.system;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.google.gson.JsonObject;
import com.ruoyi.common.result.PlateJsonResult;
import com.ruoyi.common.result.ResultInfo;
import com.ruoyi.common.utils.exception.CustomException;
import com.ruoyi.system.service.IGetPlateNumberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 提交车牌照片，并且拿到车牌号
 * @author Xiexingxie
 * @date 2022/11/15 下午 02:11
 */

@RestController
@RequestMapping("/system/platenumber")
public class GetPlateNumberController {

    private final static Logger logger = LoggerFactory.getLogger(GetPlateNumberController.class);
    
    @Autowired
    private IGetPlateNumberService iGetPlateNumberService;
    
    @PostMapping("/upload-platenumber")
    @ResponseStatus(HttpStatus.CREATED)
    public String plateJsonResult(MultipartFile file){
        String flag =iGetPlateNumberService.licensePlate(file);
        JSONObject jsonObject = new JSONObject(flag.toString());
        String words_result = jsonObject.getStr("words_result");
        JSONObject jsonObject1 = new JSONObject(words_result.toString());
        String number = jsonObject1.getStr("number");
        logger.debug("识别到的车牌号为：{}",number);
        if (flag == null){
            throw new CustomException(20001, ResultInfo.PLATENUMBER_RECOGNITION_FAIL);
        }
        
        return number;
    }

}
